package com.ruoyi.wisdom.task;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.wisdom.domain.swap.SwapSchoolHeadteacher;
import com.ruoyi.wisdom.event.SchoolHeadteacherEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchoolHeadteacherTask extends IotTask {

    @Autowired
    private SchoolHeadteacherEvent schoolHeadteacherEvent;

    @Scheduled(fixedRate = 10000) // 每10秒执行一次
    public void pullTask() {
        try {
            Long length = redisSecondTemplate.opsForList().size(iotConstants.getPullSchoolHeadteacher());
            if (length > 0) {
                while (length > 0) {
                    Object obj = redisSecondTemplate.opsForList().leftPop(iotConstants.getPullSchoolHeadteacher());
                    if (obj != null) {
                        String objJson = obj.toString();
                        SwapSchoolHeadteacher swapSchoolHeadteacher = JSON.parseObject(objJson, SwapSchoolHeadteacher.class);
                        schoolHeadteacherEvent.handle(swapSchoolHeadteacher);
                    }
                    length--;
                }
            }
        } catch (Exception e) {
            printErrorLog("拉取班主任数据-- redis 队列-异常：" + e.getMessage());
        }
    }
}
